package invalidcomponentgeneralization;

import java.util.Arrays;
import org.eclipse.viatra2.emf.incquery.runtime.api.IPatternMatch;
import org.eclipse.viatra2.emf.incquery.runtime.api.impl.BasePatternMatch;
import org.eclipse.viatra2.patternlanguage.core.patternLanguage.Pattern;
import sysmodel.BusinessComponent;
import sysmodel.RestComponent;

/**
 * Pattern-specific match representation of the InvalidComponentGeneralization pattern, 
 * to be used in conjunction with InvalidComponentGeneralizationMatcher.
 * 
 * <p>Class fields correspond to parameters of the pattern. Fields with value null are considered unassigned.
 * Each instance is a (possibly partial) substitution of pattern parameters, 
 * usable to represent a match of the pattern in the result of a query, 
 * or to specify the bound (fixed) input parameters when issuing a query.
 * 
 * @see InvalidComponentGeneralizationMatcher
 * @see InvalidComponentGeneralizationProcessor
 * 
 */
public final class InvalidComponentGeneralizationMatch extends BasePatternMatch implements IPatternMatch {
  private RestComponent fComponent1;
  
  private BusinessComponent fComponent2;
  
  private static String[] parameterNames = {"component1", "component2"};
  
  public InvalidComponentGeneralizationMatch(final RestComponent pComponent1, final BusinessComponent pComponent2) {
    this.fComponent1 = pComponent1;
    this.fComponent2 = pComponent2;
    
  }
  
  @Override
  public Object get(final String parameterName) {
    if ("component1".equals(parameterName)) return this.fComponent1;
    if ("component2".equals(parameterName)) return this.fComponent2;
    return null;
    
  }
  
  public RestComponent getComponent1() {
    return this.fComponent1;
    
  }
  
  public BusinessComponent getComponent2() {
    return this.fComponent2;
    
  }
  
  @Override
  public boolean set(final String parameterName, final Object newValue) {
    if ("component1".equals(parameterName) && newValue instanceof sysmodel.RestComponent) {
    	this.fComponent1 = (sysmodel.RestComponent) newValue;
    	return true;
    }
    if ("component2".equals(parameterName) && newValue instanceof sysmodel.BusinessComponent) {
    	this.fComponent2 = (sysmodel.BusinessComponent) newValue;
    	return true;
    }
    return false;
    
  }
  
  public void setComponent1(final RestComponent pComponent1) {
    this.fComponent1 = pComponent1;
    
  }
  
  public void setComponent2(final BusinessComponent pComponent2) {
    this.fComponent2 = pComponent2;
    
  }
  
  @Override
  public String patternName() {
    return "InvalidComponentGeneralization";
    
  }
  
  @Override
  public String[] parameterNames() {
    return InvalidComponentGeneralizationMatch.parameterNames;
    
  }
  
  @Override
  public Object[] toArray() {
    return new Object[]{fComponent1, fComponent2};
    
  }
  
  @Override
  public String prettyPrint() {
    StringBuilder result = new StringBuilder();
    result.append("\"component1\"=" + prettyPrintValue(fComponent1) + ", ");
    
    result.append("\"component2\"=" + prettyPrintValue(fComponent2)
    );return result.toString();
    
  }
  
  @Override
  public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((fComponent1 == null) ? 0 : fComponent1.hashCode()); 
    result = prime * result + ((fComponent2 == null) ? 0 : fComponent2.hashCode()); 
    return result; 
    
  }
  
  @Override
  public boolean equals(final Object obj) {
    if (this == obj)
    	return true;
    if (obj == null)
    	return false;
    if (!(obj instanceof IPatternMatch))
    	return false;
    IPatternMatch otherSig  = (IPatternMatch) obj;
    if (!pattern().equals(otherSig.pattern()))
    	return false;
    if (!InvalidComponentGeneralizationMatch.class.equals(obj.getClass()))
    	return Arrays.deepEquals(toArray(), otherSig.toArray());
    				InvalidComponentGeneralizationMatch other = (InvalidComponentGeneralizationMatch) obj;
    				if (fComponent1 == null) {if (other.fComponent1 != null) return false;}
    				else if (!fComponent1.equals(other.fComponent1)) return false;
    				if (fComponent2 == null) {if (other.fComponent2 != null) return false;}
    				else if (!fComponent2.equals(other.fComponent2)) return false;
    				return true;
    
  }
  
  @Override
  public Pattern pattern() {
    return InvalidComponentGeneralizationMatcher.FACTORY.getPattern();
  }
}
